var api = require('../../utils/api.js');
var app = getApp();
Page({
    data: {
        systemInfo: {},
        list: [],
        loadingMore: false,
        noMoreData: false
    },
    currentPageNumber: 1,
    currentAssetUc: "",
    onLoad() {
        try {
            var isLogin = wx.getStorageSync('login');
            if (!isLogin) {
                wx.showToast({
                    title: '请先授权登录',
                    icon: 'loading',
                    duration: 600
                });
                setTimeout(function () {
                    //navigateTo
                    wx.switchTab({ url: '/pages/my/my'});
                }, 600);
                return;
            }
        } catch (e) {
        }
    },
    onShow() {
        if (app.pagesNeedUpdate['pages/index/index'] == 1) {
            let newItems = api.updatePageList('id');
            this.setData({
                list: newItems
            });
        }
        this.onPullDownRefresh();
        this.pullUpLoad();

        api.pageNeedUpdate('pages/index/index', 0);
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
        this.currentPageNumber = 1;
        this.setData({
            noMoreData: false,
            noData: false
        });
        api.get({
            url: 'logi/UserLogis/my',
            data: {
                order:'-published_time'
            },
            success: data => {
                let newItems = api.updatePageList('id', data.data, this.formatListItem, true);
                this.setData({
                    list: newItems
                });

                if (data.data.length > 0) {
                    this.currentPageNumber++;
                } else {
                    this.setData({
                        noMoreData: true
                    });

                    // 没有数据
                    // this.setData({
                    //     noMoreData: true,
                    //     noData: true
                    // });
                }

                wx.stopPullDownRefresh();
            }
        });
    },

    /**
     * 上拉刷新
     */
    pullUpLoad() {
        if (this.data.loadingMore || this.data.noMoreData) return;
        this.setData({
            loadingMore: true
        });
        wx.showNavigationBarLoading();

        api.get({
            url: 'logi/Userlogis/my',
            data: {
                page: this.currentPageNumber,
                order:'-published_time'
            },
            success: data => {
                let newItems = api.updatePageList('id', data.data, this.formatListItem);
                //console.log(newItems);
                this.setData({
                    list: newItems
                });
                if (data.data.length > 0) {
                    this.currentPageNumber++;
                } else {
                    this.setData({
                        noMoreData: true
                    });
                }
            },
            complete: () => {
                this.setData({
                    loadingMore: false
                });
                wx.hideNavigationBarLoading();
            }
        });
    },
    formatListItem(item) {
        if (item.Thumbnail) {
            item.Thumbnail = api.getUploadUrl(item.Thumbnail);
        }
        return item;
    },
    onReachBottom() {
        this.pullUpLoad();
    },
    onListItemTap(e) {
        let id = e.currentTarget.dataset.id;

        wx.navigateTo({
            url: '/pages/logi/logi?id=' + id
        });

    },
    onListItemMoreTap(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/logi/logi?id=' + id
        });
    },
    previewImage(e) {
        wx.previewImage({
            current: '', // 当前显示图片的http链接
            urls: [e.currentTarget.dataset.previewUrl] // 需要预览的图片http链接列表
        });

    },
    onShareAppMessage() {
        return {
            title: '樱花驿站',
            path: '/pages/index/index'
        }
    }

});
